package com.itheima.controller;

import com.itheima.domain.Teacher;
import com.itheima.service.TeacherService;

import java.util.Scanner;

/*
老师 用户交互类
键盘录入
输出语句

增
删
改
查
 */
public class TeacherController {
    // 调用Service 的新增方法
    private TeacherService teacherService = new TeacherService();
    private Scanner sc = new Scanner(System.in);

    // 老师系统的入口
    public void start() {

        //死循环
        loop:
        while (true) {
            //键盘录入
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            String choice = sc.next();
            //switch
            switch (choice) {
                case "1":
                    addTeacher();
                    break;
                case "2":
                    deleteTeacher();
                    break;
                case "3":
                    updateTeacher();
                    break;
                case "4":
                    findAllTeachers();
                    break;
                case "5":
                    System.out.println("回到主菜单");
                    //break loop;
                    return;
                default:
                    System.out.println("输入有误");
            }
        }
    }

    public void updateTeacher() {
        Teacher[] teachers = teacherService.findAllTeachers();
        if (teachers == null) {
            System.out.println("没有数据,请先添加");
            return;
        }
        String sid = getSid();
        // 继续录入其他信息
        Teacher teacher = getTeacherInfo(sid);
        teacherService.updateTeacher(sid,teacher);
        System.out.println("修改成功");
    }

    public Teacher getTeacherInfo(String sid) {
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        System.out.println("请输入生日");
        String birthday = sc.next();
        return new Teacher(sid, name, age, birthday);
    }

    public String getSid() {
        //录入学号
        System.out.println("请输入学号");
        String sid=null;
        while (true) {
            sid= sc.next();
            //判断学号是否存在
            boolean exists = teacherService.isExists(sid);
            //存在
            if (exists) {
                break;
            } else { //不存在 重新录入
                System.out.println("不存在,请重新录入");
            }
        }
        return sid;
    }

    public void deleteTeacher() {
        Teacher[] teachers = teacherService.findAllTeachers();
        if (teachers == null) {
            System.out.println("没有数据,请先添加");
            return;
        }
        String sid = getSid();
        //删除
        teacherService.deleteTeacher(sid);
        System.out.println("删除成功");

    }

    public void findAllTeachers() {
        //调用service findALl
        Teacher[] teachers = teacherService.findAllTeachers();
        if (teachers == null) {
            System.out.println("没有数据,请先添加");
            return;
        }
        System.out.println("学号\t姓名\t年龄\t生日");
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
            if (teacher != null) {
                System.out.println(teacher.getSid() + "\t" + teacher.getName() + "\t" + teacher.getAge()
                        + "岁\t" + teacher.getBirthday());
            }
        }


    }

    public void addTeacher() {

        //键盘录入各种信息

        System.out.println("请输入学号");
        String sid = null;
        while (true) {
            sid = sc.next();
            //调用service 方法 来 判断学号是否存在
            boolean isExists = teacherService.isExists(sid);
            if (isExists) {
                System.out.println("已经存在请重新输入");
            } else {
                //不存在
                break;
            }
        }
        Teacher teacher = getTeacherInfo(sid);

        boolean flag = teacherService.addTeacher(teacher);//如果数组满了 可能放不下 所以 需要一个返回值 返回插入成功还是失败
        if (flag) {
            System.out.println("新增成功");
        } else {
            System.out.println("添加失败");
        }

    }
    
}
